package com.example.bookkeeping1220.db;

import android.os.Build;
import android.util.Log;

import androidx.annotation.RequiresApi;

import com.example.bookkeeping1220.R;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.List;


import java.util.Arrays;
import java.util.stream.Collectors;

@RequiresApi(api = Build.VERSION_CODES.N)
public class AccountBean {
    int id;
    String typename;   //类型
    int sImageId;   //被选中类型图片
    String beizhu;   //备注
    float money;  //价格
    String time;  //保存时间字符串
    int year;
    int month;
    int day;
    int kind;   //类型  收入---1   支出---0

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTypename() {
        return typename;
    }

    public void setTypename(String typename) {
        this.typename = typename;
    }

    public int getsImageId() {
        return sImageId;
    }

    public void setsImageId(int sImageId) {
        this.sImageId = sImageId;
    }

    public String getBeizhu() {
        return beizhu;
    }

    public void setBeizhu(String beizhu) {
        this.beizhu = beizhu;
    }

    public float getMoney() {
        return money;
    }

    public void setMoney(float money) {
        this.money = money;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public int getKind() {
        return kind;
    }

    public void setKind(int kind) {
        this.kind = kind;
    }

    public AccountBean() {
    }

    public AccountBean(int id, String typename, int sImageId, String beizhu, float money, String time, int year, int month, int day, int kind) {
        this.id = id;
        this.typename = typename;
        this.sImageId = sImageId;
        this.beizhu = beizhu;
        this.money = money;
        this.time = time;
        this.year = year;
        this.month = month;
        this.day = day;
        this.kind = kind;
    }

    private void setInitTime() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
        Calendar calendar = Calendar.getInstance();
        time = sdf.format(date);
        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH) + 1;
        day = calendar.get(Calendar.DAY_OF_MONTH);
    }

    static Map<String, String> identification = Arrays.stream(new Object[][]{
            {"聚餐", "餐饮"}, {"中餐", "餐饮"}, {"吃饭", "餐饮"}, {"奶茶", "餐饮"},
            {"公交", "交通"}, {"打车", "交通"}, {"高铁", "交通"}, {"地铁", "交通"}, {"车票", "交通"},
            {"淘宝", "购物"}, {"京东", "购物"}, {"购买", "购物"}, {"入手", "购物"}, {"超市", "购物"}, {"礼品", "购物"},
            {"衣服", "服饰"}, {"衣物", "服饰"}, {"裤子", "服饰"}, {"裙子", "服饰"},
            {"日用", "日用品"},
            {"电影", "娱乐"}, {"桌游", "娱乐"}, {"唱歌", "娱乐"}, {"聚会", "娱乐"}, {"游戏", "娱乐"},
            {"小吃", "零食"}, {"薯片", "零食"}, {"糖果", "零食"}, {"饮料", "零食"},
            {"资料", "学习"}, {"培训", "学习"}, {"课程", "学习"}, {"课本", "学习"}, {"文具", "学习"},
            {"打针", "医疗"}, {"开药", "医疗"}, {"药品", "医疗"}, {"体检", "医疗"},
            {"工资", "薪资"}, {"薪水", "薪资"},
            {"奖学金", "奖金"}, {"红包", "奖金"},
            {"股票", "投资"}, {"基金", "投资"}
    }).collect(Collectors.toMap(kv -> (String) kv[0], kv -> (String) kv[1]));

    static Map<String, Integer> expandImageId = Arrays.stream(new Object[][]{
            {"其他", R.mipmap.ic_qita_fs},
            {"餐饮", R.mipmap.ic_canyin_fs},
            {"交通", R.mipmap.ic_jiaotong_fs},
            {"购物", R.mipmap.ic_gouwu_fs},
            {"服饰", R.mipmap.ic_fushi_fs},
            {"日用品", R.mipmap.ic_riyongpin_fs},
            {"娱乐", R.mipmap.ic_yule_fs},
            {"零食", R.mipmap.ic_lingshi_fs},
            {"烟酒茶", R.mipmap.ic_yanjiu_fs},
            {"学习", R.mipmap.ic_xuexi_fs},
            {"医疗", R.mipmap.ic_yiliao_fs},
            {"住宅", R.mipmap.ic_zhufang_fs},
            {"水电煤", R.mipmap.ic_shuidianfei_fs},
            {"通讯", R.mipmap.ic_tongxun_fs},
            {"人情往来", R.mipmap.ic_renqingwanglai_fs},
    }).collect(Collectors.toMap(kv -> (String) kv[0], kv -> (int) kv[1]));

    static Map<String, Integer> incomeImageId = Arrays.stream(new Object[][]{
            {"薪资", R.mipmap.in_xinzi_fs},
            {"奖金", R.mipmap.in_jiangjin_fs},
            {"借入", R.mipmap.in_jieru_fs},
            {"收债", R.mipmap.in_shouzhai_fs},
            {"利息收入", R.mipmap.in_lixifuji_fs},
            {"投资回报", R.mipmap.in_touzi_fs},
            {"二手交易", R.mipmap.in_ershoushebei_fs},
            {"意外所得", R.mipmap.in_yiwai_fs},
    }).collect(Collectors.toMap(kv -> (String) kv[0], kv -> (int) kv[1]));

    public AccountBean trans(List<String> words) {
        AccountBean bean = new AccountBean();
        money = -1;
        typename = "";
        beizhu = "";
        for (String s : words) {
            try {
                money = Float.parseFloat(s);
            } catch (NumberFormatException e) {
//                e.printStackTrace();
            }
            if (identification.containsKey(s)) {
                beizhu = s;
                typename = identification.get(s);
                if (expandImageId.containsKey(typename)) {
                    kind = 0;
                    sImageId = expandImageId.get(typename);
                } else if (incomeImageId.containsKey(typename)) {
                    kind = 1;
                    sImageId = incomeImageId.get(typename);
                }
                setInitTime();
            }
        }
//        Log.e("cost", String.valueOf(money));
        if (money > 0) {
            if (typename.isEmpty()) {
                kind = 0;
                typename = "其他";
                sImageId = expandImageId.get(typename);
                setInitTime();
            }
            DBManager.insertItemToAccounttb(this);
        }

        return bean;
    }

}

